package com.white.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.white.bean.Communicate;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface CommunicateMapper extends BaseMapper<Communicate> {
    String TABLE_NAME = " communicate ";
    int LIMIT = 50;
    String CREATE_TIME = "createTime";

    /**
     * 获取交流数据
     * @param requestUserId 请求用户id
     * @param responseUserId 响应用户id
     * @return 交流数据
     */
    @Select("select * from" + TABLE_NAME +
            "where (requestUserId = #{requestUserId} and responseUserId = #{responseUserId}) " +
            "or (requestUserId = #{responseUserId} and responseUserId = #{requestUserId}) order by " + CREATE_TIME + " limit " + LIMIT)
    List<Communicate> search(Long requestUserId, Long responseUserId);

    /**
     * 查询最新聊天记录
     * @param requestUserId
     * @param responseUserId
     * @return
     */
    @Select("select * from communicate \n" +
            "where ((requestUserId = #{requestUserId} and responseUserId = #{responseUserId}) \n" +
            "or (requestUserId = #{responseUserId} and responseUserId = #{requestUserId})) \n" +
            "and createTime > (\n" +
            "select createTime\n" +
            "from chatLastTime \n" +
            "where (requestUserId = #{requestUserId} and responseUserId = #{responseUserId})\n" +
            ") order by " + CREATE_TIME + " limit " + LIMIT)
    List<Communicate> searchLatest(Long requestUserId, Long responseUserId);
}
